package penuntze.scan



import grails.test.mixin.*
import org.junit.*

import penuntze.filesystem.ScannerService;
import penuntze.media.MediaCollection;
import penuntze.media.MediaFile;
import penuntze.media.MediaObject;

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(ScannerService)
@Mock([MediaFile, MediaCollection, ScanJob])
class ScannerServiceTests {
	final String TEST_DATA_FOLDER_PATH = "test/data"
	
	void testScanFolderEmpty() {
		final String EMPTY_FOLDER_PATH = TEST_DATA_FOLDER_PATH + "/emptyFolder"
		
		assertTrue "The folder shall exist", new File(EMPTY_FOLDER_PATH).exists()
		
		ScannerService scannerService = new ScannerService()
		ScanJob scanJob = scannerService.scanFolder(EMPTY_FOLDER_PATH)
		
		while (scannerService.isScanJobRunning(scanJob)) {
			sleep 10
		}
				
		assertScanJobDone(scanJob)
		
		assertEquals "No MediaFile objects shall be added to the db", 0, MediaFile.count()
		assertEquals "No MediaCollection objects shall be added to the db", 0, MediaCollection.count()
	}
	
	void testScanFolderForVideos() {
		final String VIDEOS_FOLDER_PATH = TEST_DATA_FOLDER_PATH + "/videos"
		
		assertTrue "The folder shall exist", new File(VIDEOS_FOLDER_PATH).exists()
		
		ScannerService scannerService = new ScannerService()
		ScanJob scanJob = scannerService.scanFolder(VIDEOS_FOLDER_PATH)
		
		while (scannerService.isScanJobRunning(scanJob)) {
			sleep 10
		}
				
		assertScanJobDone(scanJob)
		
		assertEquals "The four videos files shall be added to the db", 4, MediaFile.count()
		assertEquals "the videos folder shall be added to the db", 1, MediaCollection.count()
	}
	
	private void assertScanJobDone(ScanJob scanJob) {
		scanJob.refresh()
		assertNotNull "The ScanJob may not be null", scanJob
		assertTrue "The ScanJob shall be finished by now", scanJob.finished
		assertEquals "Signal 100% now", 100, scanJob.completePercent
		assertFalse "The ScanJob shall not be running", scanJob.running
	}
	
	void testIsScanJobRunning() {
		fail("Implement")
	}
}
